package in.srain.cube.views.ptr.loadmore;

import android.content.Context;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
import android.view.View;


/**
 * RecyclerViewHeaderAndFooter also wanna endless scroll to load more data.
 */
public class LoadMoreRecycleViewContainer extends LoadMoreContainerBase {

    private RecyclerViewHeaderAndFooter mRecyclerView;

    // for memory profile
    private LinearLayoutManager mLinearLayoutManager;

    public LoadMoreRecycleViewContainer(Context context) {
        super(context);
    }

    public LoadMoreRecycleViewContainer(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();

        View view = getChildAt(0);
        mRecyclerView = (RecyclerViewHeaderAndFooter) view;
        mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {

            // for memory profile
            int mFirstVisibleItem, mVisibleItemCount, mTotalItemCount;

            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {

            }

            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
                switch (newState) {
                    case RecyclerView.SCROLL_STATE_DRAGGING://拖动中
                    {
                        getLinearLayoutManager();
                        if (mLinearLayoutManager == null) {
                            return;
                        }

                        mVisibleItemCount = recyclerView.getChildCount();
                        mTotalItemCount = mLinearLayoutManager.getItemCount();
                        mFirstVisibleItem = mLinearLayoutManager.findFirstVisibleItemPosition();

                        if ((mTotalItemCount - mVisibleItemCount) <= (mFirstVisibleItem + mVisibleThreshold)) {
                            onReachBottom();
                        }
                    }
                    break;
                }
            }
        });
    }


    @Override
    protected void addFooterView(View view) {
        if (mRecyclerView.hasFooter(view)) {
            return;
        }
        if (view.getParent() == null) {
            mRecyclerView.addFooterView(view, mRecyclerView.getFooterCount());
        }
    }

    @Override
    protected void removeFooterView(View view) {
        if (mRecyclerView.hasFooter(view)) {
            mRecyclerView.removeFooterView(view);
        }
    }

    @Override
    protected Object retrieveListView() {
        return getRecyclerView();
    }

    protected LinearLayoutManager getLinearLayoutManager() {
        if (mLinearLayoutManager != null) {
            return mLinearLayoutManager;
        }
        if (mRecyclerView == null) {
            return null;
        }
        RecyclerView.LayoutManager layoutManager = mRecyclerView.getLayoutManager();
        if (layoutManager instanceof LinearLayoutManager) {
            mLinearLayoutManager = (LinearLayoutManager) layoutManager;
            return mLinearLayoutManager;
        }
        return null;
    }

    protected RecyclerView getRecyclerView() {
        return mRecyclerView;
    }

}
